package com.whr.controller;

import com.whr.bean.Node;
import com.whr.bean.Resources;
import com.whr.bean.Role;
import com.whr.bean.Times;
import com.whr.service.ResService;
import com.whr.service.RoleService;
import com.whr.utils.CompareResources;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private ResService resService;

    @RequestMapping("/list")
    public String list(Model model) {
        List<Role> roles = roleService.findAll();
        model.addAttribute("roles", roles);
        return "roleList";
    }

    @RequestMapping("/add")
    public String add(Role role) {
        roleService.saveRole(role);
        return "redirect:/role/list";
    }

    @RequestMapping("/toadd")
    public String toadd(Role role) {
        return "roleAdd";
    }

    @RequestMapping("/stop")
    public String stop(Integer[] checks) {
        roleService.stop(checks);
        return "redirect:/role/list";
    }

    @RequestMapping("/start")
    public String start(Integer[] checks) {
        roleService.start(checks);
        return "redirect:/role/list";
    }

    @RequestMapping("/toedit")
    public String toedit(Integer checks, Model model) {
        Role role = roleService.findById(checks);
        model.addAttribute("role", role);
        return "roleEdit";
    }

    @RequestMapping("/edit")
    public String edit(Role role,String rname) {
        roleService.updateRole(role,rname);
        return "redirect:/role/list";
    }

    @RequestMapping("/del")
    public String del(Integer[] checks) {
        roleService.delRole(checks);
        return "redirect:/role/list";
    }

    @ResponseBody
    @RequestMapping("/totree")
    public List<Node> totree(HttpSession session) {
        Role role = (Role) session.getAttribute("role1");
        String opath = role.getOpath();
        if(opath==null){
            opath = "";
        }
        String[] paths = opath.split(",");
        List<Resources> reses0 = resService.findAll();
        List<Node> nodes0 = new CompareResources().sortNode(reses0);
        List<Node> nodes = new ArrayList<>();

        for (Node node : nodes0) {
            for (String path : paths) {
                if (node.getPath().equals(path)){
                    node.setChecked(true);
                }
            }
            String nodename = node.getName().replaceAll("……", "");
            node.setName(nodename);
            nodes.add(node);
        }
        return nodes;
    }

    @RequestMapping("/auth")
    public String auth(Integer oid, String path) {
        roleService.updatePath(oid, path);
        return "redirect:/role/list";
    }

    @RequestMapping("/torole")
    public String torole(Integer checks, Model model, HttpSession session) {
        Role role = roleService.findById(checks);
        session.setAttribute("role1", role);
        model.addAttribute("role", role);
        return "author";
    }
    @RequestMapping("/totimes")
    public String totimes(Model model){
        Times times = roleService.findTimes();
        model.addAttribute("times", times);
        return "times";
    }
    @ResponseBody
    @RequestMapping("/upprob")
    public void upprob(Integer prob){
        roleService.upprob(prob);
    }
    @ResponseBody
    @RequestMapping("/uppass")
    public void uppass(Integer pass){
        roleService.uppass(pass);
    }
}
